<template>
  <panel-head :route="route" />
  <el-table :data="tableData.list">
    <el-table-column prop="id" label="id" width="width" />
    <el-table-column prop="name" label="名称" width="width" />
    <el-table-column prop="permissions_id" label="所属组别" width="width">
      <template #default="scope">
        {{ permissionName(scope.row.permissions_id) }}
      </template>
    </el-table-column>
    <el-table-column prop="mobile" label="手机号" width="width" />
    <el-table-column prop="active" label="状态" width="width">
      <template #default="scope">
        <el-tag class="ml-2" :type="scope.row.active ? 'success' : 'danger'">
          {{ scope.row.active ? '正常' : '禁用' }}
        </el-tag>
      </template>
    </el-table-column>
    <el-table-column prop="create_time" label="创建时间" width="width">
      <template #default="scope">
        <div class="flex-box">
          <el-icon>
            <Clock />
          </el-icon>
          <span style="margin-left: 10px;"> {{ scope.row.create_time }}</span>
        </div>
      </template>
    </el-table-column>
    <el-table-column label="操作">
      <template #default="scope">
        <el-button type="primary" @click="open(scope.row)">编辑</el-button>
        <el-button type="danger">删除</el-button>
      </template>
    </el-table-column>

  </el-table>
  <!-- 分页 -->
  <div class="pagination-info">
    <el-pagination v-model:current-page="paginationData.pageNum" size="small" :page-size="paginationData.total"
      :background="false" layout="->,total, prev, pager, next" :total="tableData.total" @size-change="handleSizeChange"
      @current-change="handleCurrentChange" />
  </div>
  <!-- 弹窗 -->
  <el-dialog v-model="dialogVisible" :before-close="beforeClose" title="添加权限" width="500" draggable>
    <el-form ref="formRef" :model="form" label-width="100px" label-position :rules="rules">
      <el-form-item label="手机号" prop="mobile">
        <el-input v-model="form.mobile" disabled></el-input>
      </el-form-item>
      <el-form-item label="昵称" prop="name">
        <el-input v-model="form.name"></el-input>
      </el-form-item>
      <el-form-item label="菜单权限" prop="permissions_id">
        <el-select v-model="form.permissions_id" placeholder="请选择">
          <el-option v-for="item in options" :key="item.id" :label="item.name" :value="item.id"></el-option>
        </el-select>
      </el-form-item>
    </el-form>
    <!-- 确认按钮 -->
    <template #footer>
      <div class="dialog-footer">
        <el-button type="primary" @click="confirm(formRef)">确认</el-button>
      </div>
    </template>
  </el-dialog>
</template>
<script setup>
import { authAdmin, menuSelectList, updateUser } from '../../../api'
import { ref, reactive, onMounted } from 'vue';
import dayjs from 'dayjs'
import { Clock } from '@element-plus/icons-vue';
import {useRoute} from 'vue-router'
const route = useRoute()


// 列表数据
const tableData = reactive({
  list: [],
  total: 0
})
// 分页数据对象
const paginationData = reactive({
  pageNum: 1,
  pageSize: 10
});
// 分页事件 
const handleSizeChange = (val) => {
  paginationData.pageSize = val
  getListData()
}
// 分页改变事件
const handleCurrentChange = (val) => {
  paginationData.pageNum = val
  getListData()
}
// 弹窗的显示隐藏状态
const dialogVisible = ref(false)
// 关闭弹窗的回调函数
const beforeClose = () => {
  // 关闭 Dialog 前执行的操作
  dialogVisible.value = false
  // 重置表单
  formRef.value.resetFields()

}
// 表单数据
const form = reactive({
  name: '',
  permissions_id: ''
})

//  表单提交
const formRef = ref();
const confirm = async (formEl) => {
  if (!formEl) return;
  await formEl.validate((valid, fields) => {
    if (valid) {
      console.log('submit!', form);
      const { name, permissions_id } = form
      updateUser({ name, permissions_id }).then(({ data }) => {
        beforeClose()
        getListData()
      })
    } else {
      console.log('error submit!', fields);
    }
  });
};

// 获取列表 
onMounted(() => {
  getListData()
  menuSelectList().then(({ data }) => {
    console.log(data, "menuSelectList");
    options.value = data.data
  })

})
const getListData = () => {
  authAdmin(paginationData).then(({ data }) => {
    console.log(data, "data");
    const { list, total } = data.data
    list.forEach((item) => {
      item.create_time = dayjs(item.create_time).format('YYYY-MM-DD')
    })
    tableData.list = list
    tableData.total = total
  })
}
// 创建响应式变量，存储
const options = ref([])
//根本权限id匹配权限名称
const permissionName = (id) => {
  const data = options.value.find(item => {
    return item.id == id
  })
  return data ? data.name : '超级管理员'
}

// 打开编辑页面
const open = (row) => {
  dialogVisible.value = true
  console.log(row);
  Object.assign(form, { name: row.name, permissions_id: row.permissions_id, mobile: row.mobile })
}
// 验证
const rules = ({
  name: [
    { required: true, message: '请输入昵称', trigger: 'blur' },
  ],
  permissions_id: [
    { required: true, message: '请选择菜单权限', trigger: 'change' },
  ],
})
</script>

<style lang="less" scoped>
.flex-box {
  display: flex;
  align-items: center;

}
</style>